package lagerbaum;

import java.awt.FlowLayout;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class LagerLesen extends JFrame{

	/**
	 * @param args
	 */
	private static List<Lager> hauptLagList = new ArrayList<Lager>();
	
	public static List<Lager> getLag() {
		return hauptLagList;
	}

	public static void setLag(List<Lager> lag) {
		LagerLesen.hauptLagList = lag;
	}

	public static void main(String[] args) {
		
		JFrame frame = new JFrame("JTree-Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		String fnLag = ".\\Daten\\Lager.txt";		
		
		neuLager(fnLag);
		loadLager(fnLag);
		
//		fillTree(lag);
		
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Lager");
	      
		for (int i = 0; i < hauptLagList.size(); i ++) {
			
			DefaultMutableTreeNode node = new DefaultMutableTreeNode(
					hauptLagList.get(i).getId() + " " + 
					hauptLagList.get(i).getName() + " " + 
					"(" + hauptLagList.get(i).getBestand() + ")");
			root.add(node);
			
			for (int j = 0; j < hauptLagList.get(i).getUnterLag().getLag().size(); j++) {
				DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(
						hauptLagList.get(i).getUnterLag().getLag().get(j).getId() + " " + 
						hauptLagList.get(i).getUnterLag().getLag().get(j).getName() + " " + 
						"(" + hauptLagList.get(i).getUnterLag().getLag().get(j).getBestand() + ")");
				node.add(node2);
				
				for (int k = 0; k < hauptLagList.get(i).getUnterLag().getLag().get(j).getUnterLag().getLag().size(); k++) {
					DefaultMutableTreeNode node3 = new DefaultMutableTreeNode(
							hauptLagList.get(i).getUnterLag().getLag().get(j).getUnterLag().getLag().get(k).getId() + " " + 					
							hauptLagList.get(i).getUnterLag().getLag().get(j).getUnterLag().getLag().get(k).getName() + " " + 
							"(" + hauptLagList.get(i).getUnterLag().getLag().get(j).getUnterLag().getLag().get(k).getBestand() + ")");
					node2.add(node3);
				}
			}
			
		}
	 
	      JTree tree = new JTree(root);
	      JScrollPane scrollPane = new JScrollPane(tree);
	      frame.setLayout(new FlowLayout());
	      frame.add(scrollPane);
	      frame.pack();
	      frame.setVisible(true);
		
//		showLager();

	}

	private static void fillTree(List<Lager> lag2) {
		
		// TODO Auto-generated method stub
	      
		
	}

	private static void showLager() {
		// TODO Auto-generated method stub
		
	}
	
	private static void neuLager(String fnLag) {
		
		int id;
		String name;
		int bestand;
		int ueberLager;
		
		id = 1;
		
		File file = new File(fnLag);
		try {
			Scanner datei = new Scanner(file);
			while (datei.hasNextLine()) {
				try {
					String datenSatz = datei.nextLine();
					Scanner sc = new Scanner (datenSatz).useDelimiter(";");
					id = (sc.nextInt());
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			System.out.printf("Datei <%s> nicht gefunden!\n", fnLag);
			// e.printStackTrace();
		}
		
		id++;
		name = "Test23";
		bestand = 209;
		ueberLager = 1;
		
		BufferedWriter out;
		try {
			out = new BufferedWriter(
			        new OutputStreamWriter(
			        new FileOutputStream( fnLag, true )));
			 		try {
			 			out.newLine();
			 			out.write(id + ";" + name + ";" + bestand + ";" + ueberLager);
				 		out.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}			 		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}            
		
		
	}

	private static void loadLager(String fnLag) {
		
		File file = new File(fnLag);
		
		try {
			Scanner datei = new Scanner(file);
			while (datei.hasNextLine()) {
				try {
					Lager l = new Lager();
					String datenSatz = datei.nextLine();
					Scanner sc = new Scanner (datenSatz).useDelimiter(";");
					l.setId(sc.nextInt());
					l.setName(sc.next());
					l.setBestand(sc.nextInt());
					l.setUeberLager(sc.nextInt());
					
					if (l.getUeberLager() == 0) {
						hauptLagList.add(l);
					} else {
						for (int i = 0; i < hauptLagList.size(); i ++) {
							if (hauptLagList.get(i).getId() == l.getUeberLager()) {       
								hauptLagList.get(i)
								.getUnterLag()
								.getLag()
								.add(l);
							} else {
								for (int j = 0; j < hauptLagList.get(i).getUnterLag().getLag().size(); j++) {
									if (hauptLagList.get(i).getUnterLag().getLag().get(j).getId() == l.getUeberLager()) {
										hauptLagList.get(i).getUnterLag().getLag().get(j).getUnterLag().getLag().add(l);
									} else {

									}
								}
							}
						}
					}
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			System.out.printf("Datei <%s> nicht gefunden!\n", fnLag);
			// e.printStackTrace();
		}
		
	}

}
